<template>
  <div class='xtx-goods-page' v-if="goodsData.categories">
    <div class="container">
      <!-- 面包屑 -->
      <GoodsBread :goodsData="goodsData" />
      <!-- 商品信息 -->
      <div class="goods-info">
        <!--左边-->
        <div class="media">
          <!-- 商品图片组件 -->
          <GoodsImage :images="goodsData.mainPictures" />
          <!-- 商品销售信息组件 -->
          <GoodsSales />
        </div>
        <!--右边-->
        <div class=" spec">
          <!-- 商品介绍组件 -->
          <GoodsName :goodsData="goodsData" />
          <!-- sku组件  -->
          <GoodsSku :goodsData="goodsData" @change="skuChange" />
          <!-- 数量选择组件 -->
          <XtxNumbox label="数量" v-model="num" :min="0" :max="goodsData.inventory" />
          <!-- 按钮组件 -->
          <XtxButton @click="insertCart" type="primary" size="middle">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <div class='goods-relevant'>
        <!-- 左边 -->
        <GoodsRelevant :goodsId="goodsData.id" />
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs />
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <!-- 24热榜 -->
          <GoodsHot :type="1" :goodsId="goodsData.id" />
          <!-- 专题推荐 -->
          <GoodsHot :type="2" :goodsId="goodsData.id" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import { ref, provide } from 'vue'
import GoodsBread from './components/goods-bread'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsRelevant from './components/goods-relevant'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import GoodsWarn from './components/goods-warn.vue'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsBread, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsRelevant, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const num = ref(1)
    const route = useRoute()
    const goodsData = ref({})
    findGoods(route.params.id).then(data => {
      goodsData.value = data.result
      console.log('总数据', data.result)
    })
    const currSku = ref(null) // 用户选择的SKU
    const skuChange = (sku) => {
      if (sku.skuId) {
        goodsData.value.price = sku.price
        goodsData.value.oldPrice = sku.oldPrice
        goodsData.value.inventory = sku.inventory
        currSku.value = sku
      } else {
        currSku.value = null
      }
    }
    provide('goodsData', goodsData)
    const store = useStore()
    const insertCart = () => {
      // 1. 数据有效性验证
      // 2. 组装购物车数据
      // 3. 调用action来保存到vuex
      if (!currSku.value) {
        return alert('请选择商品规格')
      }
      if (num.value > goodsData.value.inventory) {
        return alert('库存不足')
      }
      const cart = {
        id: goodsData.value.id, // 商品id
        skuId: currSku.value.skuId, // 有效skuid
        name: goodsData.value.name, // 商品名称
        picture: goodsData.value.mainPictures[0], // 商品图片  (非必须)
        price: currSku.value.oldPrice, // 老价格
        nowPrice: currSku.value.price, // 价格
        count: num.value, // 购买数量
        attrsText: currSku.value.specsText, // 规格文案
        selected: true, // 是否选中
        stock: currSku.value.inventory, // 商品的库存
        isEffective: true // 商品是否有效
      }
      console.log('需要添加的商品是', cart)
      store.dispatch('cart/inserCart', cart).then(res => {
        console.log('保存成功')
      })
    }
    return { goodsData, skuChange, num, insertCart }
  }

}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
  .large {
    width: 240px;
    height: 50px;
    font-size: 16px;
  }
  .middle {
    width: 180px;
    height: 50px;
    font-size: 16px;
  }
  .small {
    width: 100px;
    height: 32px;
    font-size: 14px;
  }
  .mini {
    width: 60px;
    height: 32px;
    font-size: 14px;
  }
  .default {
    border-color: #e4e4e4;
    color: #666;
  }
  .primary {
    border-color: @xtxColor;
    background: @xtxColor;
    color: #fff;
  }
  .plain {
    border-color: @xtxColor;
    color: @xtxColor;
    background: lighten(@xtxColor, 50%);
  }
  .gray {
    border-color: #ccc;
    background: #ccc;
    color: #fff;
  }
}
</style>
